package com.sy.service.impl;

import com.sy.dto.CartItem;
import com.sy.entity.Subject;
import com.sy.exception.BusinessException;
import com.sy.mapper.SubjectMapper;
import com.sy.service.MemberCartService;
import com.sy.utils.RedisOperations;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * description: MemberCartServiceImpl <br>
 * date: 2022/6/26 5:34 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
@Service
public class MemberCartServiceImpl extends AbstractBaseServiceImpl implements MemberCartService {
    @Autowired
    private RedisOperations redisOperations;
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public void addItem(Integer subjectId, Long count, Integer memberId) {
        if (Objects.isNull(subjectId)) {
            throw new BusinessException("商品id不能为空");
        }
        if (Objects.isNull(count)) {
            throw new BusinessException("数量不能为空");
        }
        String cartKey = "member:" + memberId;
        redisOperations.hincrby(cartKey, String.valueOf(subjectId), count);
    }

    @Override
    public List<CartItem> getCartInfo(Integer memberId) {
        String cartKey = "member:" + memberId;
        List<CartItem> cartItems = new ArrayList<>();
        if (redisOperations.exists(cartKey)) {
            //1.获取当前用户的购物车信息在Redis中所有的商品和数量
            Map<String, String> cartItemsMap = redisOperations.hgetall(cartKey);
            //2.遍历Map集合
            Set<String> keys = cartItemsMap.keySet();
            for (String key:keys) {
                CartItem cartItem = new CartItem();
                String count = cartItemsMap.get(key);
                String subjectId = key;
                cartItem.setCount(Long.parseLong(count));
                //根据课程id查询课程
                Subject subject = subjectMapper.getSubjectById(Integer.valueOf(subjectId));
                cartItem.setSubjectId(subject.getSubjectId());
                cartItem.setCoachId(subject.getCoachId());
                cartItem.setDuration(subject.getDuration());
                cartItem.setNumber(subject.getNumber());
                cartItem.setSubjectName(subject.getSubjectName());
                cartItem.setSubjectPrice(subject.getSubjectPrice());
                cartItem.setStartDate(subject.getStartDate());
                cartItem.setEndDate(subject.getEndDate());
                cartItem.setSubjectPhoto(subject.getSubjectPhoto());
                cartItems.add(cartItem);
            }
            return cartItems;
        }
        return cartItems;
    }

    @Override
    public void deleteItem(Integer memberId, Integer subjectId) {
        if (Objects.isNull(subjectId)) {
            throw new BusinessException("商品id不能为空");
        }
        String cartKey = "member:" + memberId;
        if (redisOperations.exists(cartKey)) {
            redisOperations.hdel(cartKey, String.valueOf(subjectId));
        }
    }

    @Override
    public void updateCount(Integer memberId, Integer subjectId, Long count) {
        if (Objects.isNull(subjectId)) {
            throw new BusinessException("商品id不能为空");
        }
        if (Objects.isNull(count) || count <= 0L) {
            throw new BusinessException("数值不合法");
        }
        String cartKey = "member:" + memberId;
        if (redisOperations.exists(cartKey)) {
            redisOperations.hset(cartKey, String.valueOf(subjectId), String.valueOf(count));
        }
    }
}
